/**
 * SystemServiceSettingServiceImpl.java
 * @author
 * 作者 屈甲康 
 */
package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.SystemServiceSetting;
import org.jackysoft.service.AbstractIdentifiedService;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.SystemServiceSettingService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SystemServiceSettingServiceImpl extends
AbstractStringIdServiceImpl<SystemServiceSetting> implements
		SystemServiceSettingService {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3582795953355334954L;
	final static Log logger = LogFactory
			.getLog(SystemServiceSettingServiceImpl.class);

	public SystemServiceSettingServiceImpl() {
		this.type = SystemServiceSetting.class;
	}

	@Override
	public void create(Collection<SystemServiceSetting> beans)
			throws DataAccessException {

		for (SystemServiceSetting bean : beans) {
			if("NONE".equals(bean.getSysService().getId()))continue;
			SystemServiceSetting sss = get(bean.getWorkflowName(), bean.getActionId());
			if (sss == null)
				super.create(bean);
			else
				super.update(bean);
		}
	}

	@Override
	public SystemServiceSetting get(String workflow, Integer actionId) {
		String hql = String
				.format("select new SystemServiceSetting(s.workflowName,s.actionId,s.sysService) from %s s where s.workflowName='%s' and s.actionId=%d",
						entitytable(), workflow, actionId);
		Iterator itr = getSession().createQuery(hql).iterate();
		return (SystemServiceSetting) (itr.hasNext() ? itr.next() : null);
	}

	@Override
	public void remove(String wf, Integer actionId) {
		String hql = String
				.format("delete from %s where workflowName='%s' and actionId=%d",
						entitytable(), wf, actionId);
		getSession().createQuery(hql).executeUpdate();
	}

}